package com.yuyou.train.admin.controller;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import org.hibernate.validator.constraints.NotBlank;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.yuyou.train.admin.service.ICaptchaService;
import com.yuyou.train.admin.service.ICommSmsVerCodeService;
import com.yuyou.train.admin.service.ILoginService;
import com.yuyou.train.admin.vo.req.ReqLogin;
import com.yuyou.train.admin.vo.req.ReqRegisAndReset;
import com.yuyou.train.admin.vo.resp.RespLogin;
import com.yuyou.train.util.bean.ResBoolSimpleInfo;
import com.yuyou.train.util.bean.ReturnResult;
import com.yuyou.train.util.enums.admin.CommSmsVerCodeEnums;

/**
 * 
 * @author qiyong.luo
 * @date 2018-06-27
 * 
 */
@Validated
@RestController
@RequestMapping(value="/api/")
public class LoginController {

	@Autowired
	private ILoginService loginService;

	@Autowired
	private ICaptchaService captchaService;
	
	@Autowired
	private ICommSmsVerCodeService commSmsVerCodeService;

	@ApiOperation(value="注册")
	@RequestMapping(value="/register" ,method=RequestMethod.POST)
	public ReturnResult<RespLogin> register(@Validated @ModelAttribute ReqRegisAndReset reqRegisAndReset){	
		RespLogin respLogin=loginService.register(reqRegisAndReset);
		return ReturnResult.success(respLogin);		
	}
	
	@ApiOperation(value="登录")
	@RequestMapping(value="/login" ,method=RequestMethod.POST)
	public ReturnResult<RespLogin> login(@Validated @ModelAttribute ReqLogin reqLogin){
		RespLogin respLogin=loginService.login(reqLogin);
		return ReturnResult.success(respLogin);		
	}
	
	@ApiOperation(value="重置密码")
	@RequestMapping(value="/resetPass" ,method=RequestMethod.POST)
	public ReturnResult<ResBoolSimpleInfo<String>> resetPass(@Validated @ModelAttribute ReqRegisAndReset reqRegisAndReset){	
		ResBoolSimpleInfo<String> res=loginService.resetPass(reqRegisAndReset,false);
		return ReturnResult.success(res);		
	}
	
	
    @ApiOperation("发送注册登录手机验证码")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "phone", value = "手机号", paramType="query", required = true),
		@ApiImplicitParam(name = "captcha", value = "图形验证码", paramType="query",required = true),
		@ApiImplicitParam(name = "token", value = "图形验证码token", paramType="query", required = true)}
		)
	@RequestMapping(value="/sendLoginSms",method=RequestMethod.POST)
	public ReturnResult<ResBoolSimpleInfo<String>> addSmsVerCode(
			@NotBlank (message="phone不能为空")String phone,
			@NotBlank (message="captcha不能为空")String captcha,
			@NotBlank (message="token不能为空")String token){	
  	
		//图形验证码
		ResBoolSimpleInfo<String> captchaRes=captchaService.verifyCode(token, captcha);
		if(!captchaRes.isSuccess()){
			return ReturnResult.success(captchaRes);
		}
		ResBoolSimpleInfo<String> stringResBoolSimpleInfo = commSmsVerCodeService.addSmsVerCode(phone, CommSmsVerCodeEnums.type.regisAndReset.getValue());

		return ReturnResult.success(stringResBoolSimpleInfo);
	}

}
